package com.example.demo.mapper;

import com.example.demo.entity.MusicCollection;
import com.example.demo.entity.MusicQuery;
import com.example.demo.entity.NoteCollection;
import com.example.demo.vo.ViewMusicCollectionInfo;
import com.example.demo.vo.ViewNoteCollectionInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface MusicCollectionMapper {



    //歌曲收藏表与用户表的用户名、笔记表的所有内容关联,根据用户ID查找对应用户喜欢的歌曲 nc:note_collection_tb
    @Select("<script>" +
            "SELECT music_collection_tb.*, u.*, music_tb.* " +
            "FROM music_collection_tb " +
            "JOIN user_tb u ON music_collection_tb.user_id = u.user_id " +
            "LEFT JOIN music_tb ON music_collection_tb.music_id = music_tb.music_id " +
            "WHERE music_collection_tb.user_id = #{query.user_id} " +
            "<if test='query.music_name != null and query.music_name.trim() != \"\"'>" +
            "AND music_tb.music_name LIKE CONCAT('%', #{query.music_name}, '%')" +
            "</if>" +
            "<if test='query.artist != null and query.artist.trim() != \"\"'>" +
            "AND music_tb.artist LIKE CONCAT('%', #{query.artist}, '%')" +
            "</if>" +
            "<if test='query.type != null and query.type.trim() != \"\"'>" +
            "AND music_tb.type LIKE CONCAT('%', #{query.type}, '%')" +
            "</if>" +
            "</script>")
    List<ViewMusicCollectionInfo> findMusicCollectionInfoId(@Param("query") MusicQuery query);




    // 根据用户ID查询用户收藏的歌曲ID列表
    @Select("SELECT music_id FROM music_collections WHERE user_id = #{user_id}")
    List<Integer> findMusicIdsByUserId(@Param("user_id") Integer user_id);

    // 根据用户ID统计收藏歌曲种类数量
    @Select("SELECT m.type, COUNT(*) as count FROM music_tb m " +
            "JOIN music_collection_tb mc ON m.music_id = mc.music_id " +
            "WHERE mc.user_id = #{user_id} " +
            "GROUP BY m.type")
    List<Map<String, Object>> countMusicTypeByUserId(@Param("user_id") Integer user_id);



    //根据用户id和笔记id，收藏笔记。
    @Insert("INSERT INTO music_collection_tb (music_id, user_id) VALUES (#{music_id}, #{user_id})")
    int insertMusicCollection(MusicCollection musicCollection);

    //删除收藏，笔记详情页
    @Delete("delete from music_collection_tb where user_id=#{user_id} and music_id = #{music_id}")
    int deleteMusicCollection1(@Param("user_id") Integer user_id, @Param("music_id") Integer music_id);

    //检查用户是否已经收藏过某条笔记
    @Select("SELECT COUNT(*) FROM music_collection_tb WHERE user_id = #{user_id} AND music_id = #{music_id}")
    int checkIfMusicCollection(@Param("user_id") Integer user_id, @Param("music_id") Integer music_id);


}